<?php

declare(strict_types=1);

namespace App\Amqp\Consumer;

use Hyperf\Amqp\Annotation\Consumer;
use Hyperf\Amqp\Message\ConsumerMessage;
use Hyperf\Amqp\Result;
use PhpAmqpLib\Message\AMQPMessage;
use Hyperf\Amqp\Message\Type;
use App\Model\PssLogModel\MessageCallBackNotifyLog;

/**
 * qos (prefetch_size,prefetch_count,global)
 * @Consumer(exchange="NOTIFY_MESSAGE_CALLBACK", routingKey="NOTIFY_MESSAGE_CALLBACK", queue="NOTIFY_MESSAGE_CALLBACK", nums=1)
 */
class WebNotifyMessageConsumer extends ConsumerMessage
{
    protected $type = Type::DIRECT;//直连模式
    protected $qos  = [
        'prefetch_count'    => 10
    ];
    public function consumeMessage($data, AMQPMessage $message): string
    {
        try {
            MessageCallBackNotifyLog::instance()->addLog($data);
            logger('info','WebNotifyMessageConsumerSuccess')->info("consumer success");
            return Result::ACK;
        } catch ( \Throwable $t ) {
            logger('error','WebNotifyMessageConsumerError')->info($t->getMessage());
            sleep(3);//休眠3秒之后，重回队列
            return Result::REQUEUE;
        }
    }
    
    /**
     * 设置是否自启动
     */
    public function isEnable() : bool
    {
        $isDisAable = env('AMQP_WEB_NOTIFY_MESSAGE_CONSUMER_DISABLE');
        return $isDisAable ? false : true ;
    }
}